﻿using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Blog.API.Tool.Configuration
{
    public static class ConfigurationExtension
    {

        /// <summary>
        /// 加载配置文件
        /// </summary>
        /// <param name="fileName">文件名称</param>
        /// <param name="environmentName">环境名称</param>
        /// <param name="reloadOnChange">自动更新</param>
        /// <returns></returns>


        public static string GetConfig(this IConfiguration configuration, params string[] sections)
        {
            try
            {
                if (sections.Any())
                {
                    return configuration[string.Join(":", sections)];
                }
            }
            catch (Exception ex)
            {
                //Logger.Error($"{ToKey(sections)} 不存在", ex);
                //Serilog.Log.Error(ex, $"{ToKey(sections)} 不存在", null);
            }

            return "";
        }

        public static T GetSection<T>(this IConfiguration configuration, params string[] keys) where T : class
        {
            try
            {

                if (keys.Any())
                {
                    return configuration.GetSection(ToKey(keys)).Get<T>();
                }
            }
            catch (Exception ex)
            {
                //Logger.Error($"{ToKey(keys)}: 不存在", ex);
            }

            return default;
        }

        private static string ToKey(params string[] keys)
        {
            return string.Join(":", keys);
        }
    }
}
